﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Model.CSP;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class ContactWayDao : CSPCommonDao, IContactWayDao
    {
        public ContactWayDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }


        public ICollection<ContactWay> FindByUser(string userName)
        {
            var result = _context.ContactWays.Where(c => c.username == userName);
            if (result.Count() != 0)
            {
                return result.ToArray();
            }
            else
            {
                return null;
            }
        }



        public ICollection<Model.CSP.ContactWay> FindAll()
        {
            var result = _context.ContactWays.ToArray();
            if (result.Count() != 0)
            {
                return result.ToArray();
            }
            else
            {
                return null;
            }
        }

        public Model.CSP.ContactWay GetDataById(int id)
        {
            return _context.ContactWays.FirstOrDefault(a => a.id == id);
        }



        public ContactWay GetDataByIdAndUser(int Id, string userName)
        {
            return _context.ContactWays.FirstOrDefault(a => a.id == Id && a.username == userName);
        }

        public ICollection<Model.CSP.ContactWay> GeDatatByIds(int[] Ids)
        {
            return _context.ContactWays.Where(a => Ids.Contains(a.id)).ToArray();
        }

        public void Add(Model.CSP.ContactWay obj)
        {
            _context.ContactWays.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.ContactWay obj)
        {
            ContactWay contactWay = GetDataById(obj.id);
            if (contactWay != null)
            {
                contactWay = obj;
            }
            _context.SaveChanges();
        }

        public ContactWay DeleteById(int id)
        {
            ContactWay contactWay = GetDataById(id);
            _context.ContactWays.Remove(contactWay);
            _context.SaveChanges();
            return contactWay;
        }

        public ContactWay DeleteByIdAndUser(int id, string userName)
        {
            ContactWay contactWay = _context.ContactWays.Where(c => c.id == id && c.username == userName).FirstOrDefault();
            _context.ContactWays.Remove(contactWay);
            _context.SaveChanges();
            return contactWay;
        }

        public ICollection<ContactWay> FindByUsers(List<string> userNames)
        {
            ICollection<ContactWay> result = new List<ContactWay>();

            var contactWays = _context.ContactWays.Where(c => userNames.Contains(c.username)).ToArray();
            if (contactWays.Count() != 0)
            {
                result = contactWays;
            }

            return result;
        }

        /// <summary>
        /// 根据用户获取数量
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int GetCountByUser(string userName)
        {
            return _context.ContactWays.Count(c => c.username==userName);
        }
    }
}
